﻿Shader "Custom/NewSurfaceShader"
{
	Properties 
	{
		_SeaColor("SeaColor", Color) = (1,1,1,1)
		_SkyColor("SkyColor", Color) = (1,1,1,1)
		_FoamTex("FoamTex",2D)="white"{}
		_FoamThreshold("FoamThreshold",float)=0.3
		_FoamStrength("FoamStrength",Range(0,1))=1
	}
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		LOD 200
		Cull off
		
		CGPROGRAM
		#pragma surface surf Lambert
		#pragma enable_d3d11_debug_symbols

		float3 _SeaColor, _SkyColor;
		sampler2D _FresnelLookUp;
		sampler2D _FoamTex;
		sampler2D _UseFoamTex;
		float _FoamThreshold;
		float _FoamStrength;
		
		struct Input 
		{
			float3 worldNormal;
			float3 worldPos;
			float3 worldRefl;
			float2 uv_FoamTex;
		};
		
		float Fresnel(float3 V, float3 N)
		{
			float costhetai = abs(dot(V, N));
			return tex2D(_FresnelLookUp, float2(costhetai, 0.0)).a;
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			float3 V = normalize(_WorldSpaceCameraPos-IN.worldPos);
			float3 N = IN.worldNormal;
			
			float fresnel = Fresnel(V, N);
			
			o.Albedo = lerp(_SeaColor, _SkyColor, fresnel);

			float4 useFoam=tex2D(_UseFoamTex,IN.uv_FoamTex);
			float foamStrength=saturate(useFoam-_FoamThreshold)*_FoamStrength;
			float3 foam=tex2D(_FoamTex,IN.uv_FoamTex);

			o.Albedo=lerp(o.Albedo,foam,foamStrength);

			o.Alpha = 1.0;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
